package com.gherasoft.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.gherasoft.adapter.EditCheckListAdapter;
import com.gherasoft.model.CheckListItem;
import com.gherasoft.model.CheckListModel;
import com.gherasoft.model.CheckListModelListener;

import java.util.Date;

/**
 * User: sckomoroh
 * Date: 3/12/13
 * Time: 9:59 PM
 */
public class EditListActivity extends Activity
{
    private EditCheckListAdapter adapter;
    private boolean isNewList;
    private EditText listNameEditText;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_check_list);

        ImageButton saveButton = (ImageButton)findViewById(R.id.saveButton);
        Button addItemButton = (Button)findViewById(R.id.addItemButton2);
        ListView listView = (ListView)findViewById(R.id.listView);
        TextView listStateTextView = (TextView) findViewById(R.id.listStateTextView);
        this.listNameEditText = (EditText)findViewById(R.id.listNameEditText);

        Intent intent = getIntent();
        int index = intent.getIntExtra("model_index", -1);
        isNewList = index == -1;

        if (index == -1)
        {
            adapter = new EditCheckListAdapter(this);
        }
        else
        {
            CheckListModel model = MainActivity.getModel(index);
            adapter = new EditCheckListAdapter(this, model);
            listNameEditText.setText(model.getListName());
        }

        CheckListModel model = adapter.getModel();

        listView.setAdapter(adapter);
        OnAddItemListener addItemListener = new OnAddItemListener(adapter);

        model.setListener(new CheckListChangedListener(listStateTextView));
        listView.setOnItemLongClickListener(new ItemLongClickListener(model));
        saveButton.setOnClickListener(new SaveButtonClickListener());
        addItemButton.setOnClickListener(addItemListener);

        if (model.getLastSaveTime() == null)
        {
            listStateTextView.setText("Unsaved");
        }
        else
        {
            listStateTextView.setText(model.getLastSaveTime().toLocaleString());
        }
    }

    @Override
    public void onBackPressed()
    {
        CheckListModel model = adapter.getModel();
        int index = MainActivity.findModelIndex(model);

        if (!model.isSaved())
        {
            AlertDialog dialog = createSaveConfirmDialog(index);
            dialog.show();
        }
        else
        {
            goBack(index);
            super.onBackPressed();
        }
    }

    private AlertDialog createSaveConfirmDialog(int index)
    {
        AlertDialog.Builder alt_bld = new AlertDialog.Builder(EditListActivity.this);
        alt_bld.setMessage("The list is not saved, do you want to save it?")
                .setCancelable(false)
                .setPositiveButton("Yes", new SaveConfirmYesButtonListener(index))
                .setNegativeButton("No", new SaveConfirmNoButtonListener());

        AlertDialog alert = alt_bld.create();
        alert.setTitle("Save list");

        return alert;
    }

    private void emptyNameAlert()
    {
        Toast toast = Toast.makeText(this, "Could not to save list with empty name", Toast.LENGTH_SHORT);
        toast.show();
    }

    private void goBack(int index)
    {
        if (isNewList)
        {
            if (index != -1)
            {
                Intent intent = new Intent(EditListActivity.this, RunListActivity.class);
                intent.putExtra("model_index", index);
                startActivity(intent);
            }
        }
    }

    private void saveCheckList(CheckListModel model)
    {
        model.saveList(this.getApplicationInfo().dataDir);

        int index = MainActivity.findModelIndex(model);

        if (index == -1)
        {
            MainActivity.addModel(model);
        }

        Toast toast = Toast.makeText(this, "The list has been saved", Toast.LENGTH_SHORT);
        toast.show();
    }

    private Dialog createRenameDialog(int index, CheckListModel model)
    {
        CheckListItem listItem = model.getItem(index);
        Dialog dialog = new Dialog(EditListActivity.this);
        dialog.setContentView(R.layout.rename_dialog);
        dialog.setTitle("Rename item");

        EditText newNameEditText = (EditText) dialog.findViewById(R.id.newNameEditText);
        newNameEditText.setText(listItem.getTitle());

        Button renameButton = (Button) dialog.findViewById(R.id.renameButton);
        Button cancelButton = (Button) dialog.findViewById(R.id.editCancelButton);

        renameButton.setOnClickListener(new RenameDialogYesButtonListener(newNameEditText, dialog, index));
        cancelButton.setOnClickListener(new RenameDialogCancelButtonListener(dialog));

        return dialog;
    }

    private class CheckListChangedListener implements CheckListModelListener
    {
        private TextView textView;

        public CheckListChangedListener(TextView textView)
        {
            this.textView = textView;
        }

        @Override
        public void onModelChanged()
        {
            this.textView.setText("Unsaved");
        }

        @Override
        public void onModelSaved(Date saveDate)
        {
            this.textView.setText(saveDate.toLocaleString());
        }
    }

    private class OnAddItemListener implements View.OnClickListener
    {
        private EditCheckListAdapter adapter;

        public OnAddItemListener(EditCheckListAdapter adapter)
        {
            this.adapter = adapter;
        }

        @Override
        public void onClick(View view)
        {
            int index = this.adapter.addItem("new item");
            Dialog dialog = createRenameDialog(index, this.adapter.getModel());
            dialog.show();
        }
    }

    private class SaveButtonClickListener implements View.OnClickListener
    {
        @Override
        public void onClick(View view)
        {
            String listName = listNameEditText.getText().toString();
            if (listName.isEmpty())
            {
                emptyNameAlert();
                return;
            }

            adapter.getModel().setListName(listName);
            saveCheckList(adapter.getModel());
        }
    }

    private class SaveConfirmYesButtonListener implements DialogInterface.OnClickListener
    {
        private int index;

        public SaveConfirmYesButtonListener(int index)
        {
            this.index = index;
        }

        @Override
        public void onClick(DialogInterface dialog, int id)
        {
            String listName = listNameEditText.getText().toString();
            if (listName.isEmpty())
            {
                emptyNameAlert();
                return;
            }

            adapter.getModel().setListName(listName);

            saveCheckList(adapter.getModel());
            goBack(index);
            EditListActivity.super.onBackPressed();
        }
    }

    private class SaveConfirmNoButtonListener implements DialogInterface.OnClickListener
    {
        @Override
        public void onClick(DialogInterface dialog, int id)
        {
            dialog.cancel();
            EditListActivity.super.onBackPressed();
        }
    }

    private class ItemLongClickListener implements AdapterView.OnItemLongClickListener
    {
        private CheckListModel model;

        public ItemLongClickListener(CheckListModel model)
        {
            this.model = model;
        }

        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int index, long l)
        {
            Dialog dialog = createRenameDialog(index, model);
            dialog.show();

            return false;
        }
    }

    private class RenameDialogYesButtonListener implements View.OnClickListener
    {
        private EditText editText;
        private Dialog dialog;
        private int index;

        public RenameDialogYesButtonListener(EditText editText, Dialog dialog, int index)
        {
            this.editText = editText;
            this.dialog = dialog;
            this.index = index;
        }

        @Override
        public void onClick(View view)
        {
            String itemName = editText.getText().toString();
            adapter.setItem(index, itemName);
            dialog.dismiss();
        }
    }

    private class RenameDialogCancelButtonListener implements View.OnClickListener
    {
        private Dialog dialog;

        public RenameDialogCancelButtonListener(Dialog dialog)
        {
            this.dialog = dialog;
        }

        @Override
        public void onClick(View view)
        {
            dialog.dismiss();
        }
    }
}
